package cn.javacc.core.util;

import java.util.ArrayList;
import java.util.List;

public class Where {
	private String sqlStr;
	@SuppressWarnings("rawtypes")
	private List params = new ArrayList();

	private Where() {

	}

	public static Where get() {
		return new Where();
	}

	@SuppressWarnings("unchecked")
	public Where like(String field, Object value) {
		if (isNull(value)) {
			return this;
		} else {
			if (sqlStr == null) {
				sqlStr = field + " like ?";
			} else {
				sqlStr = sqlStr + " and " + field + " like ?";
			}

			params.add("%" + value + "%");
			return this;
		}
	}

	@SuppressWarnings("unchecked")
	public Where eq(String field, Object value) {
		if (isNull(value)) {
			return this;
		} else {
			if (sqlStr == null) {
				sqlStr = field + " = ?";
			} else {
				sqlStr = sqlStr + " and " + field + " = ?";
			}

			params.add(value);
			return this;
		}
	}

	@SuppressWarnings("unchecked")
	public Where le(String field, Object value) {
		if (isNull(value)) {
			return this;
		} else {
			if (sqlStr == null) {
				sqlStr = field + " <= ?";
			} else {
				sqlStr = sqlStr + " and " + field + " <= ?";
			}

			params.add(value);
			return this;
		}
	}

	@SuppressWarnings("unchecked")
	public Where ge(String field, Object value) {
		if (isNull(value)) {
			return this;
		} else {
			if (sqlStr == null) {
				sqlStr = field + " >= ?";
			} else {
				sqlStr = sqlStr + " and " + field + " >= ?";
			}

			params.add(value);
			return this;
		}
	}

	public boolean isNull(Object obj) {
		if (obj == null || obj.toString().trim().equals("")) {
			return true;
		}
		return false;
	}

	public String getSqlStr() {
		return sqlStr;
	}

	public void setSqlStr(String sqlStr) {
		this.sqlStr = sqlStr;
	}

	@SuppressWarnings("rawtypes")
	public List getParams() {
		return params;
	}

	@SuppressWarnings("rawtypes")
	public void setParams(List params) {
		this.params = params;
	}

}
